#include "VertexFormats.fx"
#include "Fog.fx"

float4x4 world_view_proj : WORLD_VIEW_PROJECTION;
float3 camera_pos : CAMERA_POS;
float time : TIME;

float fog_start = 20.0;
float fog_end = 50.0;

float start_time = 24.0f;
float fadein_length = 2.0f;

struct VSOut
{
	float4 pos : POSITION;
	float4 color : COLOR;
};

float2 rotate(float2 v, float angle) {
	float s, c;
	sincos(angle, s, c);
	return float2(c * v.x - s * v.y, s * v.x + c * v.y);
}	

VSOut VS_TunnelRects(VertexPosColor input)
{
	VSOut output;
	float t = saturate((time - start_time) / fadein_length);	
	input.pos.yz = 
		rotate(input.pos.yz, time / 2.1 + 
		sin(input.pos.x/20 - time)*cos(time*3.14*2*0.535714)*0.5);
	input.pos.yz /= smoothstep(0, 1, t);
	output.pos = mul(float4(input.pos, 1), world_view_proj);
	output.color = float4(quadraticFog(input.pos, camera_pos, input.color.xyz,
		float3(0, 0, 0), fog_start, fog_end), 1);
	output.color.w = t * 0.9;
	return output;
}

VSOut VS_TunnelRectsOuter(VertexPosColor input)
{
	VSOut output;
	float t = saturate((time+1 - start_time) / fadein_length);
	input.pos.yz *= -2;
	input.pos.yz = rotate(input.pos.yz, time / -20.8);
	input.pos.yz /= smoothstep(0, 1, t);
	input.pos.x *= 1.2;
	output.pos = mul(float4(input.pos, 1), world_view_proj);
	output.color = float4(quadraticFog(input.pos, camera_pos, input.color.xyz * 0.7,
		float3(0, 0, 0), fog_start, fog_end), 1);
	output.color.w = t * 0.9;	
	return output;
}

float4 PS_TunnelRects(VSOut input) : COLOR
{
	return input.color;
}

technique t0
{
	pass p0
	{
		VertexShader = compile vs_2_0 VS_TunnelRects();
		PixelShader = compile ps_2_0 PS_TunnelRects();

		AlphaBlendEnable = true;
		SrcBlend = srcAlpha;
		DestBlend = invSrcAlpha;
		ZEnable = false;
		CullMode = None;
	}
}

technique t1
{
	pass p0
	{
		VertexShader = compile vs_2_0 VS_TunnelRectsOuter();
		PixelShader = compile ps_2_0 PS_TunnelRects();

		AlphaBlendEnable = true;
		SrcBlend = srcAlpha;
		DestBlend = invSrcAlpha;
		ZEnable = false;
		CullMode = None;
	}
}
